import { Component, OnInit, inject } from '@angular/core';
import { TrackLineService } from '../service/track-line.service';

import { g } from '../service/global-data';

@Component({
    selector: 'app-track-info',
    providers: [TrackLineService],
    templateUrl: './track-info.html',
})
export class TrackInfo implements OnInit {
    TrackLineService = inject(TrackLineService);

    is_editing = () => g.editing;

    track_line: any = null;
    current_point: any;
    current_point_index = -1;
    current_point_time = '';

    way_point: any = null;
    photo: any = null;

    ngOnInit(): void {
        g.track_info = this;
    }

    refresh() {
        if (g.editing) {
            if (!!g.track_edit.selected_track_line) {
                this.TrackLineService.compute_edit_track_line_args(g.track_edit.selected_track_line);
                this.track_line = g.track_edit.selected_track_line;
            } else {
                this.track_line = null;
            }
        } else {
            if (!!g.user_data.selected && g.user_data.selected.type === 'track_line' && g.user_data.selected.visible) {
                this.way_point = null;
                this.photo = null;
                this.track_line = g.user_data.selected;
                if (g.track_chart.selected_point >= 0) {
                    this.current_point_index = g.track_chart.selected_point;
                    this.current_point = g.track_line_points_cache.get(this.track_line.uuid)[this.current_point_index];
                    if (this.track_line.has_timestamp) {
                        this.current_point_time = this.TrackLineService.utc2local(this.current_point[3]);
                    }
                } else {
                    this.current_point = null;
                }
            } else if (!!g.user_data.selected && g.user_data.selected.type === 'way_point' && g.user_data.selected.visible) {
                this.track_line = null;
                this.photo = null;
                this.way_point = g.user_data.selected;
                if (!!this.way_point.timestamp) {
                    this.current_point_time = this.TrackLineService.utc2local(this.way_point.timestamp);
                }
            } else if (!!g.selected_photo) {
                this.track_line = null;
                this.way_point = null;
                this.photo = g.selected_photo;
                if (!!this.photo.timestamp) {
                    this.current_point_time = this.TrackLineService.utc2local(this.photo.timestamp);
                }
            } else {
                this.track_line = null;
                this.way_point = null;
                this.photo = null;
            }         
        }
    }

}

